/*
 * liense自己看着办吧，能在代码说明引用了此项目就不错了
 */
package com.kris;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 工具类
 *
 * @author kris
 */
public class Utils {

    public static String formatDate(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.S");
        return sdf.format(new Date(time));
    }

    public static String replacePathPrefix(String path, String oldPrefix, String newPrefix) {
        oldPrefix = removeLastSlash(oldPrefix);
        newPrefix = removeLastSlash(newPrefix);
        String newPath = newPrefix + path.substring(oldPrefix.length());
        return newPath.replace("\\", "/");
    }

    private static String removeLastSlash(String path) {
        if (path.substring(path.length() - 1).equals(File.separator)) {
            return path.substring(0, path.length() - 1);
        }
        return path;
    }

    public static boolean containsInteger(int[] arr, int x) {
        for (int e : arr) {
            if (e == x) {
                return true;
            }
        }
        return false;
    }

    // 远程路径不能是一个文件，需要把文件名去掉
    public static String removeFilename(String filePath) {
        int idx = filePath.lastIndexOf("/");
        if (idx < filePath.length()) {
            return filePath.substring(0, idx);
        }
        return filePath;
    }

    public static void main(String[] args) {
//        System.out.println(formatDate(System.currentTimeMillis()));
//        System.out.println(replacePathPrefix("D:\\Opzooncloud\\Opzoon-web\\Opzoon-UI\\vmanager\\WebContent\\A.txt", 
//                "D:\\Opzooncloud\\Opzoon-web\\Opzoon-UI\\vmanager\\WebContent", "/etc"));
        System.out.println(removeFilename("/etc/network/script.sh"));
    }
}
